package com.zx.notification.producer.components;

import com.zx.core.wrapper.ResponseWrapper;
import com.zx.notification.common.enums.BizTypeEnum;
import com.zx.notification.common.model.SystemNotifications;
import com.zx.notification.common.param.SystemNotificationsParam;
import com.zx.notification.common.utils.NotificationNoUtils;
import com.zx.notification.producer.service.ISystemNotificationsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * <p>
 * description: 通知组件，用于处理通用通知发送逻辑 <br>
 * create: 2025-10-17
 * </p>
 *
 * @author zhou  xun
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class NotificationComponent {

    private final ISystemNotificationsService systemNotificationsService;

    private static SystemNotificationsParam getSystemNotificationsParam(SystemNotifications systemNotifications) {
        SystemNotificationsParam param = new SystemNotificationsParam();
        param.setId(systemNotifications.getId());
        param.setUserId(systemNotifications.getUserId());
        param.setBizType(systemNotifications.getBizType());
        param.setTemplateCode(systemNotifications.getTemplateCode());
        param.setTitle(systemNotifications.getTitle());
        param.setContent(systemNotifications.getContent());
        param.setTargetChannels(systemNotifications.getTargetChannels());
        param.setCreateTime(systemNotifications.getCreateTime());
        return param;
    }

    /**
     * 发送通用通知
     *
     * @param userId         用户ID
     * @param bizType        业务类型
     * @param templateCode   模板编码
     * @param title          通知标题
     * @param content        通知内容
     * @param targetChannels 目标渠道
     * @return {@link ResponseWrapper}
     */
    public ResponseWrapper sendGeneralNotification(String userId, BizTypeEnum bizType, String templateCode,
                                                   String title, String content, String targetChannels) {
        ResponseWrapper result = new ResponseWrapper();
        try {
            // 构建系统通知参数对象
            com.zx.notification.common.param.SystemNotificationsParam param = new com.zx.notification.common.param.SystemNotificationsParam();
            param.setId(NotificationNoUtils.generateNotificationId());
            param.setUserId(userId);
            param.setBizType(bizType.getCode());
            param.setTemplateCode(templateCode);
            param.setTitle(title);
            param.setContent(content);
            param.setTargetChannels(targetChannels);

            // 调用服务添加通知
            result = systemNotificationsService.addSystemNotification(param);
        } catch (Exception e) {
            log.error("发送通用通知时发生错误", e);
            result.fail("发送通知失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 发送系统通知
     *
     * @param systemNotifications 系统通知对象
     * @return {@link ResponseWrapper}
     */
    public ResponseWrapper sendSystemNotification(SystemNotifications systemNotifications) {
        ResponseWrapper result = new ResponseWrapper();
        try {
            // 设置ID和创建时间
            if (systemNotifications.getId() == null || systemNotifications.getId().isEmpty()) {
                systemNotifications.setId(NotificationNoUtils.generateNotificationId());
            }
            if (systemNotifications.getCreateTime() == null) {
                systemNotifications.setCreateTime(LocalDateTime.now());
            }

            // 构建系统通知参数对象
            SystemNotificationsParam param = getSystemNotificationsParam(systemNotifications);

            // 调用服务添加通知
            result = systemNotificationsService.addSystemNotification(param);
        } catch (Exception e) {
            log.error("发送系统通知时发生错误", e);
            result.fail("发送系统通知失败: " + e.getMessage());
        }
        return result;
    }
}